package com.lrs.java.dataStructure.simpleSort;

class SelectArray {

	private long[] arr;
	private int nElems;
	
	public SelectArray(int capacity) {
		arr = new long[capacity];
	}

	public int size() {
		return nElems;
	}

	public void insert(int value) {
		arr[nElems] = value;
		nElems++;
	}

	public void selectionSort() {
		int out, in, minIdx;
		
		for(out=0; out<nElems-1; out++) {
			minIdx = out;
			
			for(in=out+1; in<nElems; in++) {
				if(arr[in] < arr[minIdx]) {
					minIdx = in;
				}
			}
			
			if(minIdx != out) {
				swap(out, minIdx);
			}
		}
	}
	
	private void swap(int out, int minIdx) {
		long temp = arr[out];
		arr[out] = arr[minIdx];
		arr[minIdx] = temp;
	}

	public void display() {
		for(int i=0; i<nElems; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}
	
}

class SelectSortApp {
	public static void main(String[] args) {
		
		SelectArray selArr = new SelectArray(10);
		
		
		selArr.insert(10);
		selArr.insert(70);
		selArr.insert(0);
		selArr.insert(40);
		
		selArr.display();
		
		selArr.selectionSort();
		
		selArr.display();
		
	}
}
